<?php

namespace app\admin\controller;

use app\api\model\ConfigPersonal;
use app\api\model\Privatechat;
use think\Db;
use think\Session;
use app\api\model\Task;

use phpspreadsheet\src\PhpSpreadsheet\Spreadsheet;
use phpspreadsheet\src\PhpSpreadsheet\Style\Alignment;
use phpspreadsheet\src\PhpSpreadsheet\Writer\Xlsx;

class Platform extends Common
{
    public function balance()
    {
        $where = ['id' => ['gt', 0]];

        $data = Db::name('platform_balance')
            ->where($where)->order('id desc')
            ->paginate(_getCachePages(), false, ['query' => request()->param()]);

        $this->assign('data', $data);
        $page = $data->render();
        $this->assign('page', $page);

        return $this->fetch();
    }

    public function bill()
    {
        $where = ['id' => ['gt', 0]];

        $data = Db::name('platform_bill')
            ->where($where)->order('id desc')
            ->paginate(_getCachePages(), false, ['query' => request()->param()])/*->each(function ($item, $key) {
                $start = strtotime($item['date']);
                $end = $start + 86399;
                $item['income'] = Db::name('topup')->where(['state' => 1, 'addtime' => ['between', [date('Y-m-d H:i:s', $start), date('Y-m-d H:i:s', $end)]]])->sum('price');
                $item['expense'] = Db::name('yaoqing_records t1')
                    ->join('user t2', 't2.id = t1.uid')
                    ->where([
                        't1.type' => 1,
                        't1.data' => 0,
                        't1.content' => ['like', '%收益'],
                        't1.time' => ['between', [$start, $end]],
                        't2.sex' => '女',
                        't2.is_identity_authentication' => 2
                    ])->sum('money');

                return $item;
            })*/;

        $this->assign('data', $data);
        $page = $data->render();
        $this->assign('page', $page);

        return $this->fetch();
    }

    public function topup()
    {
        $where = [];
        $subQuery = Db::table('topup')
            ->field('sum(price) as total, fk_userid')
            ->where('type!=3 and state!=2')
            ->group('fk_userid')
            ->order('total', 'desc')
            ->buildSql();
        $data = Db::table($subQuery.' a')
                ->where($where)
                ->order('total', 'desc')
                ->paginate(_getCachePages(), false, ['query' => request()->param()])
                ->each(function($item){

                    $user = Db::name('user')->where(['id'=>$item['fk_userid']])->find();

                    $recycle_userids_a = [];
                    $recycle_userids_b = [];
                    $recycle_userids_c = [];
                    $recycle_userids_d = [];
                    $recycle_userids_e = [];
                    $recycle_userids_f = [];
                    $recycle_userids_g = [];
                    $recycle_userids_h = [];
                    $recycle_userids_i = [];
                    if($user['device_id']){
                        $recycle_userids_a = Db::name('recycle_user')->where(['data' => ['like', '%'.$user['device_id'].'%']])->column('user_id');
                        $recycle_userids_g = Db::name('user')->where(['device_id' => $user['device_id']])->column('id');
                    }
                    if($user['zhifucount']){
                        $recycle_userids_b = Db::name('recycle_user')->where(['data' => ['like', '%'.$user['zhifucount'].'%']])->column('user_id');
                        $recycle_userids_e = Db::name('user')->where(['zhifucount' => $user['zhifucount']])->column('id');
                        $recycle_userids_i = Db::name('withdraw')->where(['data' => ['like', '%'.$user['zhifucount'].'%']])->column('userid');
                    }
                    if($user['zhifuname']){
                        $recycle_userids_c = Db::name('recycle_user')->where(['data' => ['like', '%'.$user['zhifuname'].'%']])->column('user_id');
                        $recycle_userids_f = Db::name('user')->where(['zhifuname' => $user['zhifuname']])->column('id');
                    }
                    if($user['phone']){
                        $recycle_userids_d = Db::name('recycle_user')->where(['phone' => ['like', '%'.$user['phone'].'%']])->column('user_id');
                        $recycle_userids_h = Db::name('withdraw')->where(['data' => ['like', '%'.$user['phone'].'%']])->column('userid');
                    }

                    $allids = array_unique(array_merge($recycle_userids_a, $recycle_userids_b, $recycle_userids_c , $recycle_userids_d, $recycle_userids_e, $recycle_userids_f, $recycle_userids_g, $recycle_userids_h, $recycle_userids_i));

                    $item['count'] = $item['total'];
                    if(sizeof($allids) > 1){
                        $item['count'] = Db::table('topup')->where(['fk_userid'=>['in', $allids]])->sum('price');
                    }

                    return $item;
                });

        $this->assign('data', $data);
        $page = $data->render();
        $this->assign('page', $page);

        return $this->fetch();
    }
}
